package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.RyFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by ${wushenglu} on 2018/5/21.
 */
@Controller
@RequestMapping(ControllerConstants.FRIENDSHIP)
public class FriendShipController extends BaseMobileController {
    @Autowired
    private RyFriendService ryFriendService;

    /**
     * 添加好友
     * 10: 请求, 11: 被请求, 20: 同意, 21: 忽略, 30: 被删除
     */
    @RequestMapping("/invite")
    public void addIncidental(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");//发起好友请求的用户融云编号
                String friendId = getString(json, "friendId", "");//被发起好友请求的用户融云编号
                String message = getString(json, "message", "");//发起好友请求的备注信息
                String user = getString(json, "user", "");//发起好友请求的用户信息{"id":"slEcpCI63","nickname":"一杯水","region":"86","phone":"13269772766","portraitUri":"http://7xogjk.com1.z0.glb.clouddn.com/Fo6wxS7zzvGpwyAFhlpTUVirpOGh"}
                String friend = getString(json, "friend", "");//被发起好友请求的用户信息{"id":"slEcpCI63","nickname":"一杯水","region":"86","phone":"13269772766","portraitUri":"http://7xogjk.com1.z0.glb.clouddn.com/Fo6wxS7zzvGpwyAFhlpTUVirpOGh"}
                return ryFriendService.invite(userId, friendId, message, user, friend);
            }
        });
    }

    /**
     * 同意好友
     */
    @RequestMapping("/agree")
    public void agree(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");//当前登录账号的融云编号
                String friendId = getString(json, "friendId", "");//发起好友请求的用户融云编号
                return ryFriendService.agree(userId, friendId);
            }
        });
    }

    /**
     * 忽略
     */
    @RequestMapping("/ignore")
    public void ignore(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");//当前登录账号的融云编号
                String friendId = getString(json, "friendId", "");//发起好友请求的用户融云编号
                return ryFriendService.ignore(userId, friendId);
            }
        });
    }

    /**
     * 忽略
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");//当前登录账号的融云编号
                String friendId = getString(json, "friendId", "");//发起好友请求的用户融云编号
                return ryFriendService.delete(userId, friendId);
            }
        });
    }

    /**
     * 设置备注
     */
    @RequestMapping("/setDisplayName")
    public void setDisplayName(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String friendId = getString(json, "friendId", "");//发起好友请求的用户融云编号
                String userId = getString(json, "userId", "");//自己的编号
                String displayName = getString(json, "displayName", "");//备注名
                return ryFriendService.setDisplayName(friendId, displayName, userId);
            }
        });
    }

    /**
     * 全部好友
     */
    @RequestMapping("/all")
    public void all(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");//当前登录账号的融云编号
                Integer nowPage = getInteger(json, "nowPage", 1);
                Integer pageSize = getInteger(json, "pageSize", 20);
                return ryFriendService.all(userId, nowPage, pageSize);
            }
        });
    }

    /**
     * 好友申请列表
     * 10: 请求, 11: 被请求, 20: 同意, 21: 忽略, 30: 被删除
     */
    @RequestMapping("/inviteAllList")
    public void inviteAllList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");//当前登录账号的融云编号
                Integer nowPage = getInteger(json, "nowPage", 1);
                Integer friendShipStatus = getInteger(json, "friendShipStatus", 0);
                Integer pageSize = getInteger(json, "pageSize", 20);

                return ryFriendService.inviteAllList(userId, nowPage, pageSize, friendShipStatus);
            }
        });
    }

    /**
     * 查询好友信息
     */
    @RequestMapping("/profile")
    public void profile(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String friendId = getString(json, "friendId", "");//朋友融云编号
                String userId = getString(json, "userId", "");//当前登录账号的融云编号
                return ryFriendService.profile(friendId, userId);
            }
        });
    }

    /**
     * 查询好友信息
     */
    @RequestMapping("/checkIsFriend")
    public void checkIsFriend(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String friendId = getString(json, "friendId", "");//朋友融云编号
                String userId = getString(json, "userId", "");//当前登录账号的融云编号
                return ryFriendService.checkIsFriend(friendId, userId);
            }
        });
    }

    /**
     * 查询对应编号的信息
     */
    @RequestMapping("/getUserInfoByUserId")
    public void getUserInfoByUserId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");
                String selfUserId = getString(json, "selfUserId", "");//当前登录账号的融云编号
                return ryFriendService.getUserInfoByUserId(userId, selfUserId);
            }
        });
    }

    /**
     * 查询好友
     */
    @RequestMapping("/findFriendsByAccount")
    public void findFriendsByAccount(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");//当前登录账号的融云编号
                String telPhone = getString(json, "telPhone", "");//检查的手机号
                return ryFriendService.findFriendsByAccount(userId, telPhone);
            }
        });
    }

    /**
     * 查询好友
     */
    @RequestMapping("/deleteHistory")
    public void deleteHistory(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                Integer id = getInteger(json, "id", 0);
                return ryFriendService.deleteHistory(id);
            }
        });
    }

    /**
     * 模糊查询查询好友
     */
    @RequestMapping("/findFriend")
    public void findFriend(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String json) throws ServiceException {
                String userId = getString(json, "userId", "");
                String key = getString(json, "key", "");
                return ryFriendService.findFriend(userId,key);
            }
        });
    }
}
